package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.VivodlrizhiDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Vivodenglurizhi;
import com.bdqn.zhang.pojo.Vivoly;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VivodlrizhiDaoimpl extends DButil implements VivodlrizhiDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;
    @Override
    public List<Vivodenglurizhi> getAll(int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivodenglurizhi> list = new ArrayList();
        String sql = "select * from Vivodenglurizhi limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, from);
            ment.setObject(2, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivodenglurizhi vivodenglurizhi = new Vivodenglurizhi();
                    vivodenglurizhi.setId(resultSet.getInt(1));
                    vivodenglurizhi.setBt(resultSet.getString(2));
                    vivodenglurizhi.setNr(resultSet.getString(3));
                    vivodenglurizhi.setLx(resultSet.getString(4));
                    vivodenglurizhi.setName(resultSet.getString(5));
                    vivodenglurizhi.setDatee(resultSet.getString(6));
                    list.add(vivodenglurizhi);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int getCount() {
        conn = getConnerction();
        String sql = "select count(1) from Vivodenglurizhi";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int add(Vivodenglurizhi Vivodenglurizhi) {
        String sql = "INSERT INTO Vivodenglurizhi(bt,nr,lx,name,datee) VALUES(?,?,?,?,?)";
        Object[] obj = {Vivodenglurizhi.getBt(),Vivodenglurizhi.getNr(),Vivodenglurizhi.getLx(),Vivodenglurizhi.getName(),Vivodenglurizhi.getDatee()};
        return executeUpdate(sql,obj);
    }

    @Override
    public int del(String id) {
        String sql = "DELETE from Vivodenglurizhi where id in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }
}
